package org.tech4j.edi.schedule.common;

import org.tech4j.edi.alarm.api.AlarmHandler;
import org.tech4j.edi.common.entity.RetryBizConfig;
import org.tech4j.edi.common.result.EdiResult;
import org.tech4j.edi.persistence.api.RetryConfigPersistService;
import org.tech4j.edi.persistence.api.query.RetryConfigQuery;

import java.util.List;
import java.util.Objects;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/10 16:15
 **/
public class DefaultScanRetryAlarmService implements ScanRetryAlarmService {

    private final AlarmHandler alarmHandler;
    private final RetryConfigPersistService retryConfigPersistService;

    public DefaultScanRetryAlarmService(AlarmHandler alarmHandler, RetryConfigPersistService retryConfigPersistService) {
        this.alarmHandler = alarmHandler;
        this.retryConfigPersistService = retryConfigPersistService;
    }

    @Override
    public void scanRetryAlarm(String env, List<String> bizTypeList) {
        for (String bizType : bizTypeList) {
            EdiResult<RetryBizConfig> result = retryConfigPersistService.getOneRetryConfig(
                    RetryConfigQuery.Builder.builder()
                            .bizType(bizType)
                            .env(env)
                            .build()
            );
            if (result.isFailed() || Objects.isNull(result.getData())) {
                continue;
            }
            alarmHandler.alarm(result.getData());
        }
    }
}
